/**
 * ShiroTest   PACKAGE_NAME
 *
 * @author：黄玉刚 -成都电安惠科有限公司
 * @date： 2019/11/30 14:13
 * @Description ：
 */

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.junit.Assert;
import org.junit.Test;

/**
 * ShiroTest   PACKAGE_NAME
 *
 * @author：黄玉刚 -成都电安惠科有限公司
 * @date： 2019/11/30 14:13
 * @Description ：
 */
public class ShiroTest01 {

    @Test
    public void testHelloWorld() {
        //1、获取SecurityManager工厂，此处使用Ini配置文件初始化SecurityManager  Factory<org.apache.shiro.mgt.SecurityManager> factory =
        Factory<SecurityManager> factory =
                new IniSecurityManagerFactory("classpath:shiro.ini");

        //2、得到SecurityManager实例 并绑定给SecurityUtils

        SecurityManager securityManager = factory.getInstance();
        SecurityUtils.setSecurityManager(securityManager);

        //3、得到Subject及创建用户名/密码身份验证Token（即用户身份/凭证）

        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken("zhang", "1232");

        try {
            //4、登录，即身份验证
            subject.login(token);
        } catch (AuthenticationException e) {
            //5、身份验证失败
            System.out.println("认证失败！"+e.getMessage());
        }

        Assert.assertEquals(true, subject.isAuthenticated()); //断言用户已经登录
        System.out.println("认证成功！");
        //6、退出
        subject.logout();
    }
}
